<?php
// 广告主管理
namespace Admin\Controller;
use Think\Controller;
class PacketChannelController extends Controller{
	/*添加渠道包*/
	public function AddPacketChannel(){
		$packet_num=I('post.packet_num');

		$cuimodel=M('packet');
		$cuiresult=$cuimodel->field('packet_num')->select();
		foreach ($cuiresult as $key => $value) {
			$aaa[]=$value['packet_num'];
		}

		if(in_array($packet_num, $aaa)){
			$ajaxReturn['code'] = 201;
			$ajaxReturn['msg'] = '渠道包id已有！';
			$this->ajaxReturn($ajaxReturn);

		}else{
				//$this->isAjaxLogin();
		$id=I('post.id');
		if($id){
			$ad_id=I('post.ad_id');
			$admodel=M('advertisement');
			$advertiser_id=$admodel->field('advertiser_id')->where('id='.$ad_id)->find();

			$advertiser_id=(int)$advertiser_id['advertiser_id'];

			$data['ad_id']=$ad_id;
			$data['packet_num']=$packet_num;
			$data['advertiser_id']=$advertiser_id;
			$data['add_time']=time();
			$model=M('packet');
			$result=$model->where('id='.$id)->save($data);
			if($result){

				$ajaxReturn['code'] = 200;
				$ajaxReturn['msg'] = '添加成功！';

			}else{

				$ajaxReturn['code'] = 500;
				$ajaxReturn['msg'] = "请上架广告";

			}

			$this->ajaxReturn($ajaxReturn);
		}else{
			$ad_id=I('post.ad_id');
			$admodel=M('advertisement');
			$advertiser_id=$admodel->field('advertiser_id')->where('id='.$ad_id)->find();

			$advertiser_id=(int)$advertiser_id['advertiser_id'];

			$data['ad_id']=$ad_id;
			$data['packet_num']=$packet_num;
			$data['advertiser_id']=$advertiser_id;
			$data['add_time']=time();

			$ddmodel=M('advertisement');
			$ddresult=$ddmodel->where('id='.$ad_id)->field('shelf_status')->find();
			if($ddresult['shelf_status']=="1"){
				$model=M('packet');
				$result=$model->add($data);
			}
			
			if($result){

				$ajaxReturn['code'] = 200;
				$ajaxReturn['msg'] = '添加成功！';

			}else{

				$ajaxReturn['code'] = 500;
				$ajaxReturn['msg'] = "失败,广告下架中";

			}
		}

		

			$this->ajaxReturn($ajaxReturn);
		}
		
	}

// 广告主下拉列表
	public function ChselectList()
	{
		
		$this->isAjaxLogin();
		
		$data=M('advertiser')->field('id as advertiser_id,company_name')->select();

		if (!empty($data)) {
			$result['msg']='查询成功';
			$result['code']=200;
			$result['data']=$data;
			 
		}else{

			$result['msg']='查询失败';
			$result['code']=500;
		}
		$this->ajaxReturn($result);	 

	}

	/*展示所有的广告名称*/
	public  function  AdvertisersDisplay(){
		$this->isAjaxLogin();
		$model=M('advertisement');
		$result=$model->field('id as ad_id,ad_name')->where('is_delete="0"')->select();
		
        if($result){

            $ajaxReturn['code'] = 200;
            $ajaxReturn['msg'] = "SUCCESS";
            $ajaxReturn['data'] = $result;

        }else{

            $ajaxReturn['code'] = 500;
            $ajaxReturn['msg'] = "查询数据失败！";
        }

        $this->ajaxReturn($ajaxReturn);
	}

	/*渠道包管理列表*/
	public function  PacketChannelDisplay(){

		//$this->isAjaxLogin();
		$advertiser_id=I('post.advertiser_id/d');
		$ad_id=I('post.ad_id/d');

		$where=array();

		if(!empty($advertiser_id) && $advertiser_id!=-1) {
            $where['wm_packet.advertiser_id'] = $advertiser_id;
        }

        if(!empty($ad_id) && $ad_id!=-1) {
            $where['wm_packet.ad_id'] = $ad_id;
        }
       

		
		$model=M('packet');
		$page=I('post.page/d');
		$page_size=I('post.page_size/d');

		$count=$model->where('wm_packet.is_delete="1"')->where($where)->count();
		$total=ceil($count/$page_size);
		$limitStart=($page-1)*$page_size;

		$result=$model->join('wm_advertisement on wm_advertisement.id=wm_packet.ad_id')->field('wm_advertisement.ad_name,wm_packet.*')->where('wm_packet.is_delete="0"')->where($where)->limit($limitStart,$page_size)->select();

		foreach ($result as $key => $value) {
			$result[$key]['add_time']=date('Y-m-d',$value['add_time']);
		}

		 // if($result){

            $ajaxReturn['code'] = 200;
            $ajaxReturn['msg'] = "SUCCESS";
            $ajaxReturn['data']['list'] = $result;
            $ajaxReturn['data']['total_page'] = $count;

        // }else{

        //     $ajaxReturn['code'] = 500;
        //     $ajaxReturn['msg'] = "查询成功";
        // }

        $this->ajaxReturn($ajaxReturn);

	}

	public function DeletePacketChannel(){
		$this->isAjaxLogin();

		$id=I('post.id');
		$model=M('packet');
		$data['is_delete']="1";
		$result=$model->where('id='.$id)->delete();

		 if($result){

            $ajaxReturn['code'] = 200;
            $ajaxReturn['msg'] = "SUCCESS";
            $ajaxReturn['data'] = $result;

        }else{

            $ajaxReturn['code'] = 500;
            $ajaxReturn['msg'] = "查询删除失败！";
        }

        $this->ajaxReturn($ajaxReturn);
	}

	public function ShowBeforeUpdatePacketChannel(){
		$this->isAjaxLogin();
		$id=I('post.id');
		$model=M('packet');
		$result=$model->join('wm_advertisement on wm_advertisement.id=wm_packet.ad_id')->field('wm_packet.*,wm_advertisement.ad_name')->where('wm_packet.id='.$id)->find();

		if($result){

            $ajaxReturn['code'] = 200;
            $ajaxReturn['msg'] = "SUCCESS";
            $ajaxReturn['data'] = $result;

        }else{

            $ajaxReturn['code'] = 500;
            $ajaxReturn['msg'] = "查询删除失败！";
        }

        $this->ajaxReturn($ajaxReturn);
	}

	

    public function SearchPackageByAdvertiser(){
    	$this->isAjaxLogin();
    	$ad_id=I('post.ad_id');
    	$model=M('packet');
    	$delete['is_delete']="0";
    	if($ad_id){
    	$result=$model->where($delete)->where('ad_id='.$ad_id)->field('id as packet_id,packet_num')->select();

    }else{
    	$result=$model->field('id as packet_id,packet_num')->select();
    }

    	if($result){

			$ajaxReturn['code'] = 200;
			$ajaxReturn['msg'] = '成功！';
            $ajaxReturn['data'] = $result;

		}else{

			$ajaxReturn['code'] = 500;
			$ajaxReturn['msg'] = "失败！";

		}

		$this->ajaxReturn($ajaxReturn);
    }

    /*展示所有渠道主*/
    public function ShowChannel(){
    	$this->isAjaxLogin();
    	$model=M('channel');
    	$result=$model->field('id as channel_id,channel_name ')->where('is_delete="1"')->select();

    	if($result){

			$ajaxReturn['code'] = 200;
			$ajaxReturn['msg'] = '成功！';
            $ajaxReturn['data'] = $result;

		}else{

			$ajaxReturn['code'] = 500;
			$ajaxReturn['msg'] = "失败！";

		}

		$this->ajaxReturn($ajaxReturn);
    }

    public function CPSupdate(){
    	$id=I('post.id');
    	$model=M('cps_rule');
    	$result=$model->where('id='.$id)->find();
    	if($result){

				$ajaxReturn['code'] = 200;
				$ajaxReturn['msg'] = '成功！';
	            $ajaxReturn['data'] = $result;

			}else{

				$ajaxReturn['code'] = 500;
				$ajaxReturn['msg'] = "失败！";

			}

			$this->ajaxReturn($ajaxReturn);
    }

    public function  AddCPS(){
    	$id=I('post.id/d');

    	if($id){
	    	$start_flow=I('post.start_flow');
	    	$end_flow=I('post.end_flow');
	    	$divide=I('post.divide');

			$data['start_flow']=$start_flow;
			$data['end_flow']=$end_flow;
			$data['divide']=$divide;
			$model=M('cps_rule');
			$result=$model->where('id='.$id)->save($data);
			if($result){

				$ajaxReturn['code'] = 200;
				$ajaxReturn['msg'] = '修改成功！';

			}else{

				$ajaxReturn['code'] = 500;
				$ajaxReturn['msg'] = "修改失败！";

			}

			$this->ajaxReturn($ajaxReturn);
    	}else{
    		$packet_id=I('post.packet_id');
	    	$start_flow=I('post.start_flow');
	    	$end_flow=I('post.end_flow');
	    	$divide=I('post.divide');
	    	$channel_id=I('post.channel_id');

			$data['packet_id']=$packet_id;
			$data['start_flow']=$start_flow;
			$data['end_flow']=$end_flow;
			$data['divide']=$divide;
			$data['channel_id']=$channel_id;
			$data['is_delete']="1";
			$data['add_time']=time();
			$model=M('cps_rule');
			$result=$model->add($data);
			if($result){

				$ajaxReturn['code'] = 200;
				$ajaxReturn['msg'] = '成功！';
	            $ajaxReturn['data'] = $result;

			}else{

				$ajaxReturn['code'] = 500;
				$ajaxReturn['msg'] = "失败！";

			}

			$this->ajaxReturn($ajaxReturn);
    	}
    	//$this->isAjaxLogin();
    	
    }

    public function  EditCPS(){
    	$this->isAjaxLogin();
    	$id=I('post.id');
    	$end_flow=I('post.end_flow');
    	$divide=I('post.divide');
    	$start_flow=I('post.start_flow');

		$data['start_flow']=$start_flow;
		$data['end_flow']=$end_flow;
		$data['divide']=$divide;
		
		$model=M('cps_rule');
		$result=$model->where([
				'id'=>$id,
			])->save($data);
		if($result){

			$ajaxReturn['code'] = 200;
			$ajaxReturn['msg'] = '成功！';
            $ajaxReturn['data'] = $result;

		}else{

			$ajaxReturn['code'] = 500;
			$ajaxReturn['msg'] = "失败！";

		}

		$this->ajaxReturn($ajaxReturn);
    }

    public function ShowCPS(){
    	$this->isAjaxLogin();

    	$packet_id=I('post.packet_id');
    	$channel_id=I('post.channel_id');
    	$model=M('cps_rule');
    	$result=$model->where([
    			'channel_id'=>$channel_id,
    			'packet_id'=>$packet_id,
    		])->field('id,start_flow,end_flow,divide')->select();

    	if($result){

			$ajaxReturn['code'] = 200;
			$ajaxReturn['msg'] = '成功！';
            $ajaxReturn['data'] = $result;

		}else{

			$ajaxReturn['code'] = 500;
			$ajaxReturn['msg'] = "失败！";

		}

		$this->ajaxReturn($ajaxReturn);
    }

    public function DeleteCPS(){
    	$this->isAjaxLogin();

    	$model=M('cps_rule');
    	$id=I('post.id');
    	
    	$result=$model->where('id='.$id)->delete();
    		if($result){

			$ajaxReturn['code'] = 200;
			$ajaxReturn['msg'] = '成功！';
            $ajaxReturn['data'] = $result;

		}else{

			$ajaxReturn['code'] = 500;
			$ajaxReturn['msg'] = "失败！";

		}

		$this->ajaxReturn($ajaxReturn);
    }

    public function cuifeng($channel_id,$packet_id,$bound_time,$ad_id,$cooperation_type,$unit_price,$deduction){

    	 $time=time();
    	 $timed=$time-$bound_time;
    	 


    	 $cishu=intval($timed/86400)-1;

    	 for ($x=0; $x<=$cishu; $x++) {
    	 	$bound_timea[]=$bound_time+$x*86400;

		 } 
		 	$admo=M('advertisement');
	    	 $ad_name=$admo->where('id='.$ad_id)->getField('ad_name');
	    	 $chmo=M('channel');
	    	 $channel_name=$chmo->where('id='.$channel_id)->getField('channel_name');
	    	 $pacmo=M('packet');
	    	 $packet_num=$pacmo->where('id='.$packet_id)->getField('packet_num');
		 foreach ($bound_timea as $key => $value) {
		 	$pacresult['packet_id']=$packet_id;
	    	 $pacresult['packet_num']=$packet_num;
	    	 $pacresult['channel_name']=$channel_name;
	    	 $pacresult['channel_id']=$channel_id;
	    	 $pacresult['ad_id']=$ad_id;
	    	 $pacresult['ad_name']=$ad_name;
	    	 $pacresult['cooperation_type']=$cooperation_type;
	    	 $pacresult['unit_price']=$unit_price;
	    	 $pacresult['deduction']=$deduction;
    		$pacresult['add_time']=$bound_timea[$key];
    		$modeld=M('channel_data');
    		$modeld->add($pacresult);

		 }



	}

    /*渠道包绑定关系添加*/
    public function  AddBindRelationship(){
    	//$this->isAjaxLogin();
	    $packet_id=I('post.packet_id/d');
	    $channel_id=I('post.channel_id/d');
    	$ad_id=I('post.ad_id/d');
	    $bound_time=I('post.bound_time');


	    $timemodel=M('channel_packet');//解绑当天不能绑定
	    $timeresult=$timemodel->where('packet_id='.$packet_id)->field('bound_offtime')->select();
	    foreach ($timeresult as $key => $value) {
	    	$timesre[]=$value['bound_offtime'];
	    }
	    $pos=array_search(max($timesre),$timesre);
	    $pos=$timesre[$pos];
	    $timein=time();
	    $rrffdd=$timein-$pos;
    	$id=I('post.id/d');
    	$aaa=M('channel_packet');
    	
    	$bbb=(int)$aaa->where([
    			'bound_state'=>"1",
    			'packet_id'=>$packet_id
    		])->count();
    		$model=M('channel_packet');
    		$findsss=$model->where([
					'channel_id'=>$channel_id,
					'packet_id'=>$packet_id,
					'ad_id'=>$ad_id
				])->order('id desc')->getField('bound_offtime');
    		
    		$timenow=time();
    		$timediff=(int)$timenow-$findsss;
				
    	if($id) {
	    	$cooperation_type=I('post.cooperation_type');
	    	$deduction=I('post.deduction');
	    	$unit_price=I('post.unit_price');
			//$data['ad_id']=$ad_id;
			//$data['packet_id']=$packet_id;
			//$data['channel_id']=$channel_id;
			$data['cooperation_type']=$cooperation_type;
			$data['deduction']=$deduction;
			$data['unit_price']=$unit_price;
			//$data['bound_time']=$bound_time;
			$data['bound_state']="1";
			$result=$model->where('id='.$id)->save($data);
			
			if($result){
				$ajaxReturn['code'] = 200;
				$ajaxReturn['msg'] = 'xg成功！';
			}else{
				$ajaxReturn['code'] = 500;
				$ajaxReturn['msg'] = '渠道包ta已经绑定';
			}
    	}elseif($bbb<1  && $timediff>70000){


    		//$ad_id=I('post.ad_id/d');
    		$moddell=M('advertisement');
    		$fffdd=$moddell->where('id='.$ad_id)->find();
    		$ddv=$fffdd['shelf_status'];
	    	//$channel_id=I('post.channel_id/d');
	    	$cooperation_type=I('post.cooperation_type');
	    	$deduction=I('post.deduction');
	    	$unit_price=I('post.unit_price');
    		//$this->cuifeng($packet_id,$ad_id,$channel_id);
			$this->cuifeng($channel_id,$packet_id,$bound_time,$ad_id,$cooperation_type,$unit_price,$deduction);

			$data['ad_id']=$ad_id;
			$data['packet_id']=$packet_id;
			$data['channel_id']=$channel_id;
			$data['cooperation_type']=$cooperation_type;
			$data['deduction']=$deduction;
			$data['unit_price']=$unit_price;
			$data['bound_time']=$bound_time;
			$data['bound_state']="1";
			$model=M('channel_packet');
			if($ddv=="1"){
				$models=M('channel_packet');
				$result=$models->add($data);
				if($result){

					$ajaxReturn['code'] = 200;
					$ajaxReturn['msg'] = '成功！';
				}
			}
    	}else{
    		$ajaxReturn['code'] = 500;
			$ajaxReturn['msg'] = '渠道包已经绑定或者不能1天内重复绑定';
    	}
	$this->ajaxReturn($ajaxReturn);
    }

    /*渠道包绑定管理列表*/
    public  function PacketChannelBindDisplay(){
    	
    	$this->isAjaxLogin();

    	$ad_id=I('post.ad_id');
    	$channel_id=I('post.channel_id');
		$where=array();

		if(!empty($ad_id) && $ad_id!=-1) {
			$where= "  a.ad_id=$ad_id";
           // $where['wm_channel_packet.ad_id'] = $ad_id;
        }

        if(!empty($channel_id) && $channel_id!=-1) {
        	$where= "  a.channel_id=$channel_id";
          //  $where['wm_channel_packet.channel_id'] = $channel_id;
        }
       //echo $where;die;

		$model=M('channel_packet');
		$page=I('post.page/d');
		$page_size=I('post.page_size/d');
		$bind['bound_state']="1";

		$count=$model->alias('a')->where($bind)->where($where)->count();

		$total=ceil($count/$page_size);
		$limitStart=($page-1)*$page_size;

		
		$result=$model->alias('a')->join('wm_advertisement b on b.id=a.ad_id ')->join('wm_packet c on c.id=a.packet_id ')->join('wm_channel d on d.id=a.channel_id ')->field('a.*,b.ad_name,c.packet_num,d.channel_name')->where($bind)->where($where)->limit($limitStart,$page_size)->select();


		 // if($result){

            $ajaxReturn['code'] = 200;
            $ajaxReturn['msg'] = "SUCCESS";
            $ajaxReturn['data']['list'] = $result;
            $ajaxReturn['data']['total_page'] = $count;

        // }else{

        //     $ajaxReturn['code'] = 500;
        //     $ajaxReturn['msg'] = "查询成功";
        // }

        $this->ajaxReturn($ajaxReturn);
    }

    public function RelievePacketChannelBind(){
    	$this->isAjaxLogin();

    	$model=M('channel_packet');
    	$id=I('post.id/d');
    	$data['bound_state']="0";
    	$data['bound_offtime']=time();

    	$result=$model->where('id='.$id)->save($data);

    	$resultfind=$model->where('id='.$id)->find();
    	$channel_id=(int)$resultfind['channel_id'];
    	$packet_id=(int)$resultfind['packet_id'];
    	$pacresult=$model->alias('a')->join('wm_advertisement b on a.ad_id=b.id')->join('wm_packet d on d.id=a.packet_id')->join('wm_channel c on c.id=a.channel_id')->field('a.packet_id,a.ad_id,a.channel_id,a.cooperation_type,a.unit_price,a.deduction,a.bound_state,a.bound_time,b.ad_name,c.channel_name,d.packet_num')->where([
    			'a.packet_id'=>$packet_id,
    			'a.channel_id'=>$channel_id
    		])->find();
    	$pacresult['add_time']=time();
    	$modeld=M('channel_data');
    	$modeld->add($pacresult);
    	$remodel=M('cps_rule');
    	$res=$remodel->where([
    			'packet_id'=>$packet_id,
    			'channel_id'=>$channel_id
    		])->delete();
    	if($result ){

			$ajaxReturn['code'] = 200;
			$ajaxReturn['msg'] = '成功！';

		}else{

			$ajaxReturn['code'] = 500;
			$ajaxReturn['msg'] = "失败！";

		}

		$this->ajaxReturn($ajaxReturn);
    }

    public  function ShowBeforeUpdatePacketChannelBond(){
    	
		
    	//$this->isAjaxLogin();
        
		$model=M('channel_packet');
		
		$id=I('post.id/d');
		
		$bound_state="1";		

		
		$result=$model->alias('a')->join('wm_advertisement b on b.id=a.ad_id ')->join('wm_packet c on c.id=a.packet_id ')->join('wm_channel d on d.id=a.channel_id ')->where([
				'a'.'.'.'id'=>$id,
				'a'.'.'.'bound_state'=>$bound_state
			])->field('a.*,b.ad_name,c.packet_num,d.channel_name')->find();
	

		//$result['bound_time']=date("Y-m-d",$result['bound_time']);

		 if($result){

            $ajaxReturn['code'] = 200;
            $ajaxReturn['msg'] = "SUCCESS";
            $ajaxReturn['data'] = $result;

        }else{

            $ajaxReturn['code'] = 500;
            $ajaxReturn['msg'] = "查询b数据失败！";
        }

        $this->ajaxReturn($ajaxReturn);
    }
}